home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gnugo1_1.lha / gnugo / main.c < prev    next >
C/C++ Source or Header  |  1989-03-07  |  4KB  |  173 lines

  1. /*
  2.                 GNU GO - the game of Go (Wei-Chi)
  3.                 Version 1.1   last revised 3-1-89
  4.            Copyright (C) Free Software Foundation, Inc.
  5.                       written by Man L. Li
  6.                       modified by Wayne Iba
  7.                     documented by Bob Webber
  8. */
  9. /*
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation - version 1.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License in file COPYING for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. Please report any bug/fix, modification, suggestion to
  24.  
  25. mail address:   Man L. Li
  26.                 Dept. of Computer Science
  27.                 University of Houston
  28.                 4800 Calhoun Road
  29.                 Houston, TX 77004
  30.  
  31. e-mail address: manli@cs.uh.edu         (Internet)
  32.                 coscgbn@uhvax1.bitnet   (BITNET)
  33.                 70070,404               (CompuServe)
  34. */
  35.  
  36. #include <stdio.h>
  37.  
  38. #define EMPTY 0
  39.  
  40. unsigned char p[19][19], l[19][19], ma[19][19], ml[19][19];
  41. int mymove, umove;
  42. int rd, lib, play, pass;
  43. int mik, mjk, uik, ujk, mk, uk;  /* piece captured */
  44. int opn[9];  /* opening pattern flag */
  45.  
  46. main()
  47.   {
  48.    FILE *fp;
  49.    int i, j;
  50.    char move[10], ans[5];
  51.    int cont = 0;
  52.  
  53. /* show instruction */
  54.    showinst();
  55.  
  56.    if ((fp = fopen("gnugo.dat", "r")) != NULL)  /* continue old game */
  57.      {
  58.       cont = 1;
  59.  
  60. /* read board configuration */
  61.       for (i = 0; i < 19; i++)
  62.         for (j = 0; j < 19; j++)
  63.           fscanf(fp, "%c", &p[i][j]);
  64.  
  65. /* read my color, pieces captured */
  66.       fscanf(fp, "%d %d %d ", &mymove, &mk, &uk);
  67. /* read opening pattern flags */
  68.       for (i = 0; i < 9; i++)
  69.         fscanf(fp, "%d ", &opn[i]);
  70.  
  71.       fclose(fp);
  72.       umove = 3 - mymove;
  73.  
  74. /* delete file */
  75.       unlink("gnugo.dat");
  76.     }
  77.    else
  78.      {
  79. /* init opening pattern numbers to search */
  80.       for (i = 0; i < 9; i++)
  81.         opn[i] = 1;
  82.       opn[4] = 0;
  83.  
  84. /* init board */
  85.       for (i = 0; i < 19; i++)
  86.         for (j = 0; j < 19; j++)
  87.           p[i][j] = EMPTY;
  88. /* init global variables */
  89.       mk = 0;  uk = 0;
  90.     }
  91.  
  92. /* init global variables */
  93.    play = 1;
  94.    pass = 0;
  95.    mik = -1; mjk = -1;
  96.    uik = -1; ujk = -1;
  97.    seed(&rd);    /* start random number seed */
  98.  
  99.    if (!cont)  /* new game */
  100.      {
  101. /* ask for handicap */
  102.       printf("Number of handicap for black (0 to 17)? ");
  103.       scanf("%d", &i);
  104.       getchar();
  105.       sethand(i);
  106.  
  107. /* display game board */
  108.       showboard();
  109.  
  110. /* choose color */
  111.       printf("\nChoose side(b or w)? ");
  112.       scanf("%c",ans);
  113.       if (ans[0] == 'b')
  114.         {
  115.          mymove = 1;   /* computer white */
  116.          umove = 2;   /* human black */
  117.          if (i)
  118.        {
  119.             genmove(&i, &j);   /* computer move */
  120.             p[i][j] = mymove;
  121.           }
  122.        }
  123.       else
  124.         {
  125.          mymove = 2;   /* computer black */
  126.          umove = 1;   /* human white */
  127.          if (i == 0)
  128.        {
  129.             genmove(&i, &j);   /* computer move */
  130.             p[i][j] = mymove;
  131.           }
  132.        }
  133.     }
  134.  
  135.    showboard();
  136.  
  137. /* main loop */
  138.    while (play > 0)
  139.      {
  140.       printf("your move? ");
  141.       scanf("%s", move);
  142.       getmove(move, &i, &j);   /* read human move */
  143.       if (play > 0)
  144.     {
  145.      if (i >= 0)   /* not pass */
  146.        {
  147.         p[i][j] = umove;
  148.         examboard(mymove);     /* remove my dead pieces */
  149.       }
  150.      if (pass != 2)
  151.        {
  152.         genmove(&i, &j);   /* computer move */
  153.         if (i >= 0)   /* not pass */
  154.           {
  155.            p[i][j] = mymove;
  156.            examboard(umove);   /* remove your dead pieces */
  157.          }
  158.       }
  159.      showboard();
  160.        }
  161.       if (pass == 2) play = 0;    /* both pass then stop game */
  162.     }
  163.  
  164.  if (play == 0)
  165.    {
  166. /* finish game and count pieces */
  167.     getchar();
  168.     printf("Do you want to count score (y or n)? ");
  169.     scanf("%c",ans);
  170.     if (ans[0] == 'y') endgame();
  171.   }
  172.  }  /* end main */
  173.